package org.jivesoftware.openfire.register;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.dynamic.DbDynamicManager;
import org.jivesoftware.openfire.dynamic.DynamicPlugin;
import org.jivesoftware.openfire.interceptor.InterceptorManager;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.openfire.user.UserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;

public class RegisterPlugin implements PacketInterceptor, Plugin{
	private static final Logger log = LoggerFactory.getLogger(RegisterPlugin.class);
	 private static PluginManager pluginManager;
   private static  DbRegisterManager registerManager;
   private InterceptorManager interceptorManager;
   private static final String registerReceiver = "register";

   public RegisterPlugin(){
       interceptorManager = InterceptorManager.getInstance();
       registerManager = DbRegisterManager.getInstance();
   }

@Override
public void initializePlugin(PluginManager manager, File pluginDirectory) {
	// TODO Auto-generated method stub
	 interceptorManager.addInterceptor(this);
     pluginManager = manager;
     log.debug("RegisterPlugin  init success");
}

@Override
public void destroyPlugin() {
	// TODO Auto-generated method stub
	interceptorManager.removeInterceptor(this);
    log.debug("RegisterPlugin destroy success");
	
}

@Override
public void interceptPacket(Packet packet, Session session, boolean incoming,
		boolean processed) throws PacketRejectedException {
	// TODO Auto-generated method stub
	  JID recipient = packet.getTo();
      if (recipient != null) {
          String username = recipient.getNode();
          if (username == null || !UserManager.getInstance().isRegisteredUser(recipient)) {
              return;
          } else if (!XMPPServer.getInstance().getServerInfo().getXMPPDomain().equals(recipient.getDomain())) {
              return;
          } else if ("".equals(recipient.getResource())) {
          }
      }
      this.doAction(packet, incoming, processed, session);
}

private void doAction(Packet packet, boolean incoming, boolean processed,
		Session session) {
	// TODO Auto-generated method stub
	Packet copyPacket = packet.createCopy();
	  if (packet instanceof Message) {		
           Message message = (Message) copyPacket;
           if(message.getType()==Message.Type.chat){//single
          	 log.debug("debug","single chat"+message.toXML());
           }
         if (processed || !incoming) {
             return;
         }
         if (incoming) {       
             JID recipient = message.getTo();
             String receiver = recipient.getNode();
        
             if(receiver.equals(registerReceiver)){
            	 
            	 registerManager.addAccount(getAccount(packet, incoming, session));
            	 registerManager.addGroup(getGroupUser(packet, incoming, session));
            
         } 
  
	  }
}
}



private GroupUserBean getGroupUser(Packet packet, boolean incoming,
		Session session) {
	// TODO Auto-generated method stub
	GroupUserBean groupUserBean = new GroupUserBean();
	 Message message = (Message) packet;
	   String body = message.getBody();
	   int index1 = body.indexOf("#");
	   String username = body.substring(0,index1);
	   log.info("username:"+username);
	   groupUserBean.setUserName(username);
		groupUserBean.setGroupName("user");
		groupUserBean.setAdministrator(false);
	return groupUserBean;
}



private AccountBean getAccount(Packet packet, boolean incoming, Session session) {
	// TODO Auto-generated method stub
	   Message message = (Message) packet;
	   String body = message.getBody();
	   log.info("body:"+body);
	   int index1 = body.indexOf("#");
	   int index2 = body.indexOf("&");
	   String username = body.substring(0,index1);
	   String password = body.substring(index1+1,index2);
	   String email = body.substring(index2+1,body.length());

	   AccountBean accountBean = new AccountBean();
	   accountBean.setPlainPassword(password);
	   accountBean.setUserName(username);
	   accountBean.setEmail(email);
	   accountBean.setName(username);
	   accountBean.setCreationDate("0");
	   accountBean.setModificationDate("0");
	   return accountBean;
}
}